package com.bishe.demo.modules.other.dao;

import com.bishe.demo.modules.account.entity.User;
import com.bishe.demo.modules.common.entity.SearchBean;
import com.bishe.demo.modules.other.entity.Scenery;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface SceneryDao {
    @Insert("insert into other_scenery values(null,#{sceneryName},#{sceneryLocation},#{sceneryDetail})")
    @Options(useGeneratedKeys = true, keyProperty = "sceneryId", keyColumn = "scenery_id")
    void insertScenery(Scenery scenery);

    @Update("update other_scenery set scenery_name=#{sceneryName},scenery_location=#{sceneryLocation},scenery_detail=#{sceneryDetail} where scenery_id=#{sceneryId}")
    void updateScenery(Scenery scenery);

    @Delete("delete from other_scenery where scenery_id=#{sceneryId}")
    void deleteSceneryBySceneryId(int sceneryId);

    @Select("select * from other_scenery where scenery_id=#{sceneryId}")
    @Results(id = "sceneryResult", value = {
            @Result(column = "scenery_id", property = "sceneryId"),
            @Result(column = "scenery_id", property = "images",
                    javaType = List.class,
                    many = @Many(select = "com.bishe.demo.modules.other.dao.ImageDao.getImagesBySceneryId")),
            @Result(column = "scenery_id", property = "messages",
                    javaType = List.class,
                    many = @Many(select = "com.bishe.demo.modules.other.dao.MessageDao.getMessagesBySceneryId")),
            @Result(column = "scenery_id", property = "user",
                    javaType = User.class,
                    many = @Many(select = "com.bishe.demo.modules.account.dao.UserDao.getUserBySceneryId")),
    })
    Scenery getSceneryBySceneryId(int sceneryId);

    @Select("<script>"
            + "select * from other_scenery "
            + "<where> "
            + "<if test='keyword != \"\" and keyword != null'>"
            + " and (scenery_name like '%${keyword}%') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='orderBy != \"\" and orderBy != null'>"
            + " order by ${orderBy} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " order by scenery_id asc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    @ResultMap(value = "sceneryResult")
    List<Scenery> getSceneryBySearchBean(SearchBean searchBean);

    @Select("select * from other_scenery os left join other_scenery_user osu on osu.scenery_id=os.scenery_id where osu.user_id=#{userId}")
    @Results(id = "sceneryResult1", value = {
            @Result(column = "scenery_id", property = "sceneryId"),
            @Result(column = "scenery_id", property = "images",
                    javaType = List.class,
                    many = @Many(select = "com.bishe.demo.modules.other.dao.ImageDao.getImagesBySceneryId")),
            @Result(column = "scenery_id", property = "messages",
                    javaType = List.class,
                    many = @Many(select = "com.bishe.demo.modules.other.dao.MessageDao.getMessagesBySceneryId")),
    })
    Scenery getSceneryByUserId(int userId);
}
